import requests
'''
maokoulingClass.py 说明
            class maokouling_ONE():  我的猫口令配置信息
            |
            #根据id获得商品的高佣长链接
            def tblink_get_gaoyong(config):
            |
'''
# 每个人配置一个请求信息，要求账户设置白名单
class maokouling_ONE():
    # pid（mm_账号id_推广渠道id_推广位id）
    # mm_memberId_siteId_adzoneId
    # mm_28964573_303450418_82851750421
    itemid = ''
    userid = ''  #店铺的userid
    pid = 'mm_28964573_303450418_82851750421'  # 淘宝联盟密令 '6e39417a-bc77-3d4d-c2ae-0a51f37d2574'   # 喵口令密令
    apkey = '33e06833-f973-7708-a8aa-379b33b4c38c'  # 喵口令密令
    tbname = 'shenhuawade'
    tpwdcode = '￥OxXGby1GRvh￥'  #任意淘口令
    tpwd='1'    #是否生成淘口令，1为生成，不填则不生成
    shorturl='1'    #是否需要短链接，1为生成，不填则不生成
    extsearch ='1'  #官方查不到券时是否去第三方平台查询，1为启用
    tpwdpic=''  #商品图片链接，用于打开口令时显示，不填写默认为商品主图

# 每个人配置一个请求信息，要求账户设置白名单



#根据id获得商品的高佣长链接
def tblink_get_gaoyong(config):
    try:
        url =f'https://api.open.21ds.cn/apiv2/getitemgyurl?apkey={config.apkey}&itemid={config.itemid}&pid={config.pid}&tbname={config.tbname}'
        # url = f' https://api.open.21ds.cn/apiv1/getitemgyurl?apkey={config.apkey}&itemid={config.itemid}&pid={config.pid}&tbname={config.tbname}'
        # print(url)
        html = requests.get(url,timeout=5)
        # print(html.json())
        itemJson = html.json()
        if itemJson['code'] != 200:
            print("ID-GET-link--API状态码错误")
            return None
        else:
            itemTbkLink = itemJson['result']['data']['item_url']
            # print(itemTbkLink)
            return itemTbkLink
    except Exception as e:
        print(e)
        print('ID-GET-link白名单限制')
        return None

    #通过淘宝id查出来店铺的userid
#根据淘宝商品id 获得商铺的userid，传给userid_to_shopTBK_link获得店铺tbk链接
def item_to_SELLID(config):
    try:
        # url =f'https://api.open.21ds.cn/apiv1/getiteminfo?apkey=6e39417a-bc77-3d4d-c2ae-0a51f37d2574&itemid=568706761354'
        url= f'https://api.open.21ds.cn/apiv1/getiteminfo?apkey={config.apkey}&itemid={config.itemid}'
        print(url)
        html = requests.get(url,timeout=5)
        # print(html.json())
        itemJson = html.json()
        if itemJson['code'] != 200:
            print("ID--GET--SHOPID--API状态码错误")
            return None
        else:
            shopid = itemJson['data']['n_tbk_item']['seller_id']
            print(shopid)
            return shopid
    except Exception as e:
        print(e)
        print('ID--GET--SHOPID--白名单限制')
        return None

# 根据店铺的id获得推广商铺淘宝客链接
def userid_to_shopTBK_link(config):
    try:
        # url =f'https://api.open.21ds.cn/apiv1/doShopCovert?apkey=6e39417a-bc77-3d4d-c2ae-0a51f37d2574&pid=mm_123456_456789_789132&user_ids=123456,789456&tbname=xxxxxx'
        url= f'https://api.open.21ds.cn/apiv2/doShopCovert?apkey={config.apkey}&pid={config.pid}&user_ids={config.userid}&tbname={config.tbname}'
        # print(config.userid)
        # print(url)
        html = requests.get(url,timeout=5)
        print(html.json())
        itemJson = html.json()
        if itemJson['code'] != 200:
            print("SHOPID--GET--LINK--API状态码错误")
            return None
        else:
            shopLINK = itemJson['data'][0]['click_url']
            # print(itemTbkLink)
            return shopLINK
    except Exception as e:
        print(e)
        print('SHOPID--GET--LINK--白名单限制')
        return None

if __name__ == '__main__':
    abc = maokouling_ONE()

    abc.itemid =587776693508
    abc.userid = item_to_SELLID(abc)
    print(tblink_get_gaoyong(abc))
    print(userid_to_shopTBK_link(abc))